package com.cheer.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cheer.dao.OaIntegralDao;
import com.cheer.dto.OaIntegralDTO;
import com.cheer.entity.OaIntegralEntity;
import com.cheer.entity.OaUserEntity;
import com.cheer.exception.ServiceException;
import com.cheer.service.OaIntegralService;
import com.cheer.service.OaUserService;
import com.cheer.utils.BeanUtil;
import io.swagger.annotations.Api;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/tool")
@Api(tags = "工具类")
public class ToolController {


    @Autowired
    private OaIntegralService oaIntegralService;

    @Autowired
    private OaUserService oaUserService;

    @Autowired
    private OaIntegralDao oaIntegralDao;

    /**
     * 积分的制造
     */
    // @GetMapping("/addIntegral")
    public void addIntegral(String openId, Integer oaIntegral) {
        // 要在原来的基础上加多少分
        Integer limit = oaIntegral * 3;
        // 获取近期的活跃用户
        OaUserEntity recentOaUser = getRecentOaUser(limit);
        if(recentOaUser == null){
            throw new ServiceException("未查询到当前用户！");
        }
        String openid = recentOaUser.getOpenid();
        // 当前时间减少五天
        List<OaIntegralDTO> recentIntegral = oaIntegralService.getRecentIntegral(oaIntegral, openid);

        // 如果没找出来就再去找
        if(recentIntegral.size() < oaIntegral){
            throw new ServiceException("被抄袭用户不符合条件：" + openid);
        }
        // 获取当前用户记录
        List<OaIntegralEntity> oaIntegralEntities = oaIntegralDao.selectListByOpenid(openId);
        List<OaIntegralDTO> collect = oaIntegralEntities.stream().map(oa -> BeanUtil.transform(OaIntegralDTO.class, oa)).collect(Collectors.toList());

        // 转换为set
        Set<OaIntegralDTO> sets = new HashSet<>(recentIntegral);
        // userSet
        Set<OaIntegralDTO> userSet = new HashSet<OaIntegralDTO>(collect);

        // 删除掉重复的记录
        sets.removeAll(userSet);
        // 执行加分操作
        Integer countIntegral = 0;

        List<OaIntegralDTO> collect1 = sets.stream().collect(Collectors.toList());
        List<OaIntegralEntity> insertRecords = new ArrayList<>();
        for (int i = 0; i <collect1.size(); i++) {

            OaIntegralDTO oaIntegralDTO = collect1.get(i);
            OaIntegralEntity transform = BeanUtil.transform(OaIntegralEntity.class, oaIntegralDTO);
            transform.setOpenid(openId);
            insertRecords.add(transform);

            countIntegral+=transform.getIntegral();
            if(countIntegral >=oaIntegral){
                break;
            }
        }
        // 执行插入积分操作
        oaIntegralService.saveBatch(insertRecords);
        OaUserEntity oaUserEntity = oaUserService.getByOpenid(openId);
        Integer current = oaUserEntity.getIntegral() + countIntegral;
        oaUserEntity.setIntegral(current);
        UpdateWrapper<OaUserEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .set(OaUserEntity::getIntegral, current)
                .eq(OaUserEntity::getId, oaUserEntity.getId());
        oaUserService.updateById(oaUserEntity);
    }

    // get recent user info
    public OaUserEntity getRecentOaUser( Integer integral){

        // 真实使用 this
        DateTime dateTime = DateUtil.offsetDay(new Date(), -20);
        //DateTime dateTime = DateUtil.parse("2019-01-01", "yyyy-MM-dd");

        Long second = dateTime.getTime() / 1000;
        // 获取最值得一抄的用户积分记录
        QueryWrapper<OaUserEntity> qw = new QueryWrapper<>();
        qw.lambda().ge(OaUserEntity::getUpdataTime,second)
                .ge(OaUserEntity::getIntegral, integral)
                .orderByDesc(OaUserEntity::getAddTime).last("limit 1");
        OaUserEntity one = oaUserService.getOne(qw);
        return one;
    }

}
